Department = function() {
	return {
		
		initCompany : function() {
			$('#companyTree').tree({
				url       : '/pc/company/tree.htm',
				idField   : 'id',
				treeField : 'text',
				onClick   : function(node) {
					$('#departmentGrid').treegrid({
						title        : '企业【' + node.text + '】部门树',
						url          : '/pc/department/tree.htm?companyId=' + node.id,
						toolbar      : '#toolBar',
						onLoadSuccess : function() {
							$('#departmentGrid').treegrid('expandAll');
						}
					}).load();
				}
			});
		},
		
		initGrid : function() {
			$('#departmentGrid').treegrid({
				title        : '部门树',
			    nowrap       : false,
				rownumbers   : true,
				animate      : true,
				checkbox     : true,
				singleSelect : false,
				collapsible  : false,
				fit          : true,
				fitColumns   : true,
				idField      : 'department_id',
				treeField    : 'department_name',
			    columns      : [[
			        { field : 'department_id',        title : '部门ID',        width : 100,      checkbox : true },
			        { field : 'parent_department_id', title : '上级部门ID',    width : 100,      hidden   : true },
			        { field : 'department_name',      title : '部门名称',      width : 120 },
			        { field : 'companyName',          title : '所属企业',      width : 100 },
			        { field : 'department_address',   title : '地址',          width : 120 },
			        { field : 'empNum',               title : '员工数量(人)',  width : 60,  align : 'center' }
			    ]],
			    toolbar     : [{
					text    : '添加',
					iconCls : 'add',
					handler : function() {
						var row = $('#companyTree').tree('getSelected');
						if(null == row || "" == row) {
							Ict.info("请选择企业!");
							return;
						} else {
							var rows = $('#departmentGrid').treegrid('getSelections');
							if(rows.length > 1) {
								Ict.info("只能选择一个部门!");
								return;
							} else {
								Ict.openWin("新增部门", 450, 260, "/pc/department/add.htm");
							}
						}
					}
				}, {
					text    : '修改',
					iconCls : 'edit',
					handler : function() {
						var row = $('#companyTree').tree('getSelected');
						if(null == row || "" == row) {
							Ict.info("请选择企业!");
							return;
						} else {
							var row = $('#departmentGrid').treegrid('getSelections');
							if (0 == row.length) {
								Ict.info("请选择你要修改的部门!");
								return;
							} else if (row.length > 1) {
								Ict.info("请选择一条记录进行修改!");
								return;
							} else if (1 == row.length) {
								Ict.openWin("修改部门信息", 450, 260, "/pc/department/edit.htm?departmentId=" + row[0].department_id);
							}
						}
					}
				}, {
					text    : '删除',
					iconCls : 'remove',
					handler : function() {
						Department.remove();
					}
				}, {
					text    : '刷新',
					iconCls : 'reload',
					handler : function() {
						Department.reload();
					}
				}, {
					text    : '全部展开',
					iconCls : 'expand',
					handler : function() {
						$('#departmentGrid').treegrid('expandAll');
					}
				}, {
					text    : '全部收起',
					iconCls : 'collapse',
					handler : function() {
						$('#departmentGrid').treegrid('collapseAll');
					}
				}]
			});
		},
		
		reload : function() {
			$('#departmentGrid').treegrid('reload');
		},
		
		addSearchBox : function(width) {
			$('.searchbox').css({'width': width+'px'});
			$('.searchbox-text').css({'width': eval(width-20)+'px'});
			$('.datagrid-toolbar').append($('.serarchbox'));
			$('.datagrid-toolbar').append($('#toolbar'));
		},
		
		doSearch : function(value) {
			if("" == $.trim(value)) {
				Ict.info("请输入查询条件!");
				return;
			}
			$.ajax({
				url     : '/pc/department/search.htm',
				type    : 'POST',
				data    : {
					"deptName" : value
				},
				success : function(data) {
					$("#departmentGrid").treegrid('unselectAll');
					for(var i = 0; i < data.length; i++) {
						$("#departmentGrid").treegrid('select', data[i].department_id);
					}
				}
			});
		},
		
		remove : function() {
			var row = $('#companyTree').tree('getSelected');
			if(null == row || "" == row) {
				Ict.info("请选择企业!");
				return;
			}
			var rows = $('#departmentGrid').treegrid('getSelections');
			if(0 == rows.length) {
				Ict.info("请选择你要删除的部门!");
				return;
			} else {
				for(var i = 0; i < rows.length; i++) {
					if("Y" == rows[i].have_child_deparment) {
						Ict.info("请先删除子部门!");
						return;
					}
				}
				for(var i = 0; i < rows.length; i++) {
					if(rows[i].empNum > 0) {
						Ict.info("请先删除部门下的员工!");
						return;
					}
				}
				var departmentIds = "";
				for(var i = 0; i < rows.length; i++) {
					departmentIds += rows[i].department_id + ",";
				}
				departmentIds = departmentIds.substring(0, departmentIds.length - 1);
				Ict.confirm('确定要删除选中的部门吗?', function(btn) {
					if(btn) {
						$.post('/pc/department/remove.htm', {'departmentIds' : departmentIds}, function(data) {
							if("SUCCESS" == data) {
								$('#departmentGrid').treegrid('reload');
								Ict.info("删除成功!");
							} else {
								Ict.info("删除失败!");
							}
						});
					}
				});
			}
		},
		
		init : function() {
			Department.initCompany();
			Department.initGrid();
			Department.addSearchBox(250);
		}
	};
}();

$(function(){
	Department.init();
});